﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

namespace Orderr
{
    [Serializable]
    /*public class OrderService
    {
        public List<Order> Orders;
        public OrderService()
        {
            Orders = new List<Order>();
        }
        public void add(Order order)
        {
            var query1 = from s in Orders
                         where s.oid == order.oid
                         select s;
            if (query1.Count() == 0) Orders.Add(order);
            else throw new ArgumentException("该订单已存在");
        }
        public void delete(int oid)
        {
            var query2 = from s in Orders
                         where s.oid == oid
                         select s;
            if (query2.Count() == 0) throw new ArgumentException("该订单不存在");
            else Orders.Remove(query2.FirstOrDefault());
        }
        public void edit(Order order)
        {
            var query1 = from s in Orders
                         where s.oid == order.oid
                         select s;
            if (query1.Count() == 0) throw new ArgumentException("该订单不存在");
            if (query1.FirstOrDefault().oid != order.oid) throw new ArgumentException("订单号不能改变");
            delete(order.oid);
            add(order);
        }
        public List<Order> QueryByOid(int oid)
        {
            var query2 = from s in Orders
                         where s.oid == oid
                         select s;
            if (query2.Count() == 0) throw new ArgumentException("该订单不存在");
            return query2.ToList();
        }
        public List<Order> QueryByGood(Good good)
        {
            var query2 = from s in Orders
                         where
                            (from x in s.Details
                             where x.good.Equals(good)
                             select x).Count() > 0
                         orderby s.totalPrice
                         select s;
            if (query2.Count() == 0) throw new ArgumentException("该订单不存在");
            return query2.ToList();
        }
        public List<Order> QueryByPrice(int price)
        {
            var query2 = from s in Orders
                         where s.totalPrice >= price
                         orderby s.totalPrice
                         select s;
            if (query2.Count() == 0) throw new ArgumentException("该订单不存在");
            return query2.ToList();
        }
    }*/
    public static class DbService
    {
        public static void AddOrder(Order order)
        {
            using (var db = new OrderContext())
            {
                db.Orders.Add(order);
                db.SaveChanges();
            }
        }
        public static void AddGood(Good good)
        {
            using (var db = new OrderContext())
            {
                db.Goods.Add(good);
                db.SaveChanges();
            }
        }
        public static void Update(Order order)
        {
            Remove(order.OrderID);
            AddOrder(order);
        }
        public static void AddClient(Client client)
        {
            using (var db = new OrderContext())
            {
                db.Clients.Add(client);
                db.SaveChanges();
            }
        }
        public static void Remove(int OrderID)
        {
            using (var db = new OrderContext())
            {
                var order = db.Orders.Include("Details").Where(Order => Order.OrderID == OrderID).FirstOrDefault();
                db.Orders.Remove(order);
                db.SaveChanges();
            }
        }
        private static IQueryable<Order> AllOrders(OrderContext db)
        {
            return db.Orders.Include("Details").Where(Order => Order.Details.Count(detail => detail.Good != null) > 0); ;
        }
        public static List<Order> QueryByGood(Good good)
        {
            using (var db = new OrderContext())
            {
                var query = AllOrders(db).Where(Order => Order.Details.Count(i => i.Good.Equals(good)) > 0);
                return query.ToList();
            }
        }
        public static List<Order> QueryByPrice(int price)
        {
            using (var db = new OrderContext())
            {
                var query = AllOrders(db).Where(Order => Order.TotalPrice >= price);
                return query.ToList();
            }
        }
        public static Order QueryByOID(int OID)
        {
            using (var db = new OrderContext())
            {
                var query = AllOrders(db).Where(Order => Order.OrderID == OID);
                return query.FirstOrDefault();
            }
        }
        public static List<Order> QueryByClient(Client client)
        {
            using (var db = new OrderContext())
            {
                var query = AllOrders(db).Where(Order => Order.ClientID == client.ClientID);
                return query.ToList();
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //      Initialize
            Good good1 = new Good(1, "banana", 848);
            Good good2 = new Good(2, "apple", 666);
            Good good3 = new Good(3, "milk", 55);
            Client client1 = new Client(1, "XB");
            Client client2 = new Client(2, "TLM");
            Client client3 = new Client(3, "ZRZ");
            Order list1 = new Order(123, client1);
            list1.Details.Add(new OrderDetails(1, good1, 3));
            list1.Details.Add(new OrderDetails(2, good2, 4));
            Order list2 = new Order(234, client2);
            list2.Details.Add(new OrderDetails(3, good1, 3));
            list2.Details.Add(new OrderDetails(4, good3, 5));
            Order list3 = new Order(235, client3);
            list3.Details.Add(new OrderDetails(5, good1, 2));
            list3.Details.Add(new OrderDetails(6, good3, 4));
            list3.Details.Add(new OrderDetails(7, good2, 5));

            DbService.AddGood(good1);
            DbService.AddGood(good2);
            DbService.AddGood(good3);

            DbService.AddClient(client1);
            DbService.AddClient(client2);
            DbService.AddClient(client3);
            DbService.AddOrder(list1);
            DbService.AddOrder(list2);
            DbService.AddOrder(list3);

            Console.WriteLine(DbService.QueryByOID(234).OrderID);
            Console.ReadLine();
        }
    }
}
